package cn.woniu.ecommerceplatform.service.impl;

import cn.woniu.ecommerceplatform.entity.Address;
import cn.woniu.ecommerceplatform.entity.PageQueryParam;
import cn.woniu.ecommerceplatform.mapper.AddressMapper;
import cn.woniu.ecommerceplatform.service.AddressService;
import cn.woniu.ecommerceplatform.util.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class AddressServiceImpl implements AddressService {


    @Autowired(required = false)
    private AddressMapper addressMapper;

    @Override
    public ResponseResult<?> queryAddressList(PageQueryParam<Address> addressPageQueryParam) {
        PageHelper.startPage(addressPageQueryParam.getPageNo(),addressPageQueryParam.getPageSize());

        List<Address> address = addressMapper.queryAddress(addressPageQueryParam.getData().getAccount(),
                addressPageQueryParam.getData().getAddress());

        PageInfo pageInfo = new PageInfo(address);

        return new ResponseResult<>().ok(pageInfo);
    }

    @Override
    public ResponseResult<?> updateAddress(Address address) {
        int account = addressMapper.addressUpdate(address);
        if (account>0){
            return new ResponseResult<>().ok();
        } else {
            return new ResponseResult<>().notok();
        }
    }



    @Override
    public ResponseResult<?> addAddress(Address address) {
        return null;
    }

    @Override
    public ResponseResult<?> insertAddress(Address address) {
        Integer integer = addressMapper.insertAddressForOrder(address);
        if (integer>0){
            return new ResponseResult<>().ok();
        } else {
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> queryAddressById(Integer id) {
        Address address = addressMapper.queryAddressById(id);
        if(address != null){
            return new ResponseResult<>().ok(address);
        }else {
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> queryAddressListByAccount(PageQueryParam<Address> addressPageQueryParam) {
        PageHelper.startPage(addressPageQueryParam.getPageNo(),addressPageQueryParam.getPageSize());

        List<Address> address = addressMapper.queryAddressByAccount(addressPageQueryParam.getData().getAccount());

        PageInfo pageInfo = new PageInfo(address);

        return new ResponseResult<>().ok(pageInfo);
    }

    @Override
    public ResponseResult<?> updateAllAddress(Address address) {
        int account = addressMapper.addressAllUpdate(address);
        if (account>0){
            return new ResponseResult<>().ok();
        } else {
            return new ResponseResult<>().notok();
        }
    }


}
